package com.ruoyi.project.system.home.controller;


import com.ruoyi.framework.web.domain.Result;
import com.ruoyi.project.system.dict.utils.DictUtils;
import com.ruoyi.project.system.home.domain.*;
import com.ruoyi.project.system.home.service.MiniIBaseFamilyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/home")
public class MiniBaseFamilyController {

    @Autowired
    private MiniIBaseFamilyService miniIBaseFamilyService;


    /**
     * 首次查询展示二十一条数据
     */
    @GetMapping("/query")
    public Result listS() {
        List<MiniBaseFamilyInfoS> miniBaseFamilyInfoS = miniIBaseFamilyService.selectBaseFamilyListS();
        for (MiniBaseFamilyInfoS baseFamilyInfo : miniBaseFamilyInfoS) {
            String base_nature_village = DictUtils.getDictLabel("base_nature_village", String.valueOf(baseFamilyInfo.getNat_village()));
            baseFamilyInfo.setVillage(base_nature_village);
        }
        Integer dataSize = miniIBaseFamilyService.getDataSize();
        System.out.println(dataSize);
        if (dataSize < 21) {
            return Result.warn("数据库信息不足", miniBaseFamilyInfoS);
        }
        return Result.success(miniBaseFamilyInfoS, String.valueOf(dataSize));
    }


    /**getDictLabel
     * 实现根据姓名查找功能
     *
     * @return 如果成功则返回code码200，data为该户信息，失败则返回code码110，显示查无此人
     */
    /*@ResponseBody
    @PostMapping("/search")
    public Result search(@RequestBody MiniBaseFamilySearch miniBaseFamilySearch){
        List<MiniBaseFamilySearch> baseFamilySearches = miniIBaseFamilyService.selectBaseFamilySearch(miniBaseFamilySearch);
        List<MiniBaseFamilySearch> searchData = new ArrayList<>();
        System.out.println(miniBaseFamilySearch.getSearchContent());
        for (MiniBaseFamilySearch baseFamilySearch1 : baseFamilySearches) {
            if (baseFamilySearch1.getNat_village() == 1)
                baseFamilySearch1.setVillage("大六合");
            if (baseFamilySearch1.getNat_village() == 2)
                baseFamilySearch1.setVillage("后六合");
            if (baseFamilySearch1.getNat_village() == 3)
                baseFamilySearch1.setVillage("姜家瑶");
            if (baseFamilySearch1.getNat_village() == 4)
                baseFamilySearch1.setVillage("六家子东");
            if (baseFamilySearch1.getNat_village() == 5)
                baseFamilySearch1.setVillage("六家子西");
            if (baseFamilySearch1.getNat_village() == 6)
                baseFamilySearch1.setVillage("龙凤泡");
            if (baseFamilySearch1.getNat_village() == 7)
                baseFamilySearch1.setVillage("西石头");
            if (baseFamilySearch1.getNat_village() == 8)
                baseFamilySearch1.setVillage("小舍力");
            if ((baseFamilySearch1.getName()).equals(miniBaseFamilySearch.getSearchContent())) {
                searchData.add(baseFamilySearch1);
            }
        }
        System.out.println(searchData);
        if(searchData.size()!=0){
            return Result.success(searchData);
        }else {
            return Result.error("查无此人",110);
        }
    }*/

    /**
     * 根据输入内容进行查找
     * @param searchContent 输入信息
     * @return 查询内容
     */
    @ResponseBody
    @PostMapping("/search")
    public Result search(@RequestBody String searchContent) {
        System.out.println("搜索内容"+searchContent);
        List<MiniBaseFamilyInfoS> miniBaseFamilySearches;
        try {
            int natVillage = Integer.parseInt(DictUtils.getDictValue("base_nature_village", searchContent));
            miniBaseFamilySearches = miniIBaseFamilyService.selectBaseFamilySearchByVillage(natVillage);
        }catch (Exception e){
            miniBaseFamilySearches = miniIBaseFamilyService.selectBaseFamilySearch(searchContent);
        }
        for (MiniBaseFamilyInfoS baseFamilyInfo : miniBaseFamilySearches) {
            String base_nature_village = DictUtils.getDictLabel("base_nature_village", String.valueOf(baseFamilyInfo.getNat_village()));
            baseFamilyInfo.setVillage(base_nature_village);
        }

        if(miniBaseFamilySearches.size()==0){
            return Result.error("该用户不存在",110);
        }
        return Result.success(miniBaseFamilySearches);
    }

    /**
     * 分页请求
     *
     * @return 信息条数
     */
    @PostMapping("/queryPage")
    @ResponseBody
    public Result listTest(@RequestBody MiniPageRequest pageQuery) {
        MiniPageResult allUserByPage = miniIBaseFamilyService.getAllUserByPage(pageQuery);
        System.out.println(pageQuery.getPageNum());
        if ((long) pageQuery.getPageNum() * pageQuery.getPageSize() > allUserByPage.getTotalSize()) {
            return Result.warn("数据库信息不足", allUserByPage);
        }
        return Result.success(allUserByPage);
    }


    /**
     * 根据户编号查询基本信息
     */
    /*@PostMapping("/show")
    public Result showBase(@RequestBody MiniBaseFamilyInfoS miniBaseFamilyInfoS) {
        System.out.println(miniBaseFamilyInfoS.getFid());
        System.out.println(miniBaseFamilyInfoS.getReportYear());
        MiniBaseFamilyInfoS showUserByFid = miniIBaseFamilyService.getShowUserByFid(miniBaseFamilyInfoS.getFid(), miniBaseFamilyInfoS.getReportYear());
        if (showUserByFid.getNat_village() == 1)
            showUserByFid.setVillage("大六合");
        if (showUserByFid.getNat_village() == 2)
            showUserByFid.setVillage("后六合");
        if (showUserByFid.getNat_village() == 3)
            showUserByFid.setVillage("姜家瑶");
        if (showUserByFid.getNat_village() == 4)
            showUserByFid.setVillage("六家子东");
        if (showUserByFid.getNat_village() == 5)
            showUserByFid.setVillage("六家子西");
        if (showUserByFid.getNat_village() == 6)
            showUserByFid.setVillage("龙凤泡");
        if (showUserByFid.getNat_village() == 7)
            showUserByFid.setVillage("西石头");
        if (showUserByFid.getNat_village() == 8)
            showUserByFid.setVillage("小舍力");
        return Result.success(showUserByFid);
    }*/


}
